﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using Oracle.DataAccess.Client;
using System.Data;
using LCUV;

namespace sWarranty.WEB
{
    public partial class UserProfile : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadAuthority();
            btnClose.Attributes.Add("onclick", "CloseWindow()");
            _currentUserEmpNo = GetUserLoginEmpNo();

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");

            string m = "";
            string user = "";
            try
            {
                m = Request.QueryString["m"].ToString();
                user = Request.QueryString["u"].ToString();
            }
            catch (Exception)
            {
            }
            if (m == "1")
            {
                //Modify mode
                divModifyUser.Visible = true;
                divInfoUser.Visible = false;

                Entities db = new Entities();
                string le = "";
                string uReg = "";
                var emp = from t in db.TB_USER
                          where t.EMPNO == user
                          select t;
                if (emp.Count() == 1)
                {
                    uReg = emp.Single().REGION_ID.ToString();
                    if (!IsPostBack)
                        DBAction.SetSelected(dropUserRegion, uReg);

                    int uID = emp.Single().ID;
                    var uL = from t in db.TB_USER_LEVEL
                             where t.USER_ID == uID
                             select t;
                    if (uL.Count() == 1)
                    {
                        le = uL.Single().LEVEL_ID.ToString();
                        if (!IsPostBack)
                            DBAction.SetSelected(dropAuthority, le);
                    }
                }
                //show info
                try
                {
                    string query = "select * from VI_VN_CM_EMP_MST@dbl_mes where EMPLOYEE_NUMBER = '" + user + "'";

                    String cnn = WebConfigurationManager.ConnectionStrings["tnsString"].ConnectionString;
                    OracleCommand cmd = DBAction.con.CreateCommand();
                    cmd.CommandText = query;
                    OracleDataAdapter a = new OracleDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    a.Fill(dt);
                    cmd.Dispose();
                    if (dt.Rows.Count == 1)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                            //var u = from t in db.TB_USER where t.EMPNO == user select t;
                            lblEmpNo2.Text = user;
                            lblEmail2.Text = item["EMAIL_ADDRESS"].ToString();
                            lblDept2.Text = item["LOCATION_NAME"].ToString();
                            lblFullName2.Text = item["FULLNAME_ENGLISH"].ToString();
                            lblGrade2.Text = item["GRADE_TITLE"].ToString();
                            lblPosition2.Text = item["POSITION"].ToString();
                        }
                    }
                    else
                    {
                        ShowInfo("No User found");
                    }
                }
                catch (Exception ee)
                {
                    ShowError(ee);
                }
            }
            else
            {
                //Show user info only
                divModifyUser.Visible = false;
                divInfoUser.Visible = true;

                try
                {
                    string query = "select * from VI_VN_CM_EMP_MST@dbl_mes where EMPLOYEE_NUMBER = '" + _currentUserEmpNo + "'";

                    String cnn = WebConfigurationManager.ConnectionStrings["tnsString"].ConnectionString;
                    OracleCommand cmd = DBAction.con.CreateCommand();
                    cmd.CommandText = query;
                    OracleDataAdapter a = new OracleDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    a.Fill(dt);
                    cmd.Dispose();
                    Entities db = new Entities();
                    if (dt.Rows.Count == 1)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                            var u = from t in db.TB_USER where t.EMPNO == _currentUserEmpNo select t;
                            lblEmpNo.Text = _currentUserEmpNo;
                            lblEmail.Text = item["EMAIL_ADDRESS"].ToString();
                            lblDept.Text = item["LOCATION_NAME"].ToString();
                            lblFullName.Text = item["FULLNAME_ENGLISH"].ToString();
                            lblGrade.Text = item["GRADE_TITLE"].ToString();
                            lblPosition.Text = item["POSITION"].ToString();
                            lblAuthority.Text = _currentUserAuthority;
                            lblUserRegion.Text = DBAction.GetRegionByID(u.Single().REGION_ID);
                        }
                    }
                    else
                    {
                        ShowInfo("No User found");
                    }
                }
                catch (Exception ee)
                {
                    ShowError(ee);
                }
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void LoadAuthority()
        {
            Entities db = new Entities();
            var au = from t in db.TB_LEVEL
                     select t;
            foreach (var item in au)
            {
                ListItem i = new ListItem();
                i.Value = item.ID.ToString();
                i.Text = item.NAME + " - " + item.REMARK;
                dropAuthority.Items.Add(i);
            }

        }



        public void ShowError(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong>" + er + " </strong> </p></div></div>", false);
        }
        public void ShowError(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowInfo(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowInfo(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowWarning(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowWarning(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowSuccess(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
        }
        public void ShowSuccess(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }
        public void ShowTip(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong> " + er + "</strong> </p></div></div>", false);
        }
        public void ShowTip(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong> " + er + " </strong></p></div></div>", false);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Entities db = new Entities();
            string user = "";
            try
            {
                user = Request.QueryString["u"].ToString();
            }
            catch (Exception)
            {
            }
            try
            {
                var u = db.TB_USER.Single(f => f.EMPNO == user);
                u.REGION_ID = dropUserRegion.SelectedValue;

                int emp = u.ID;
                var uL = db.TB_USER_LEVEL.Single(f => f.USER_ID == emp);
                uL.LEVEL_ID = Convert.ToInt32(dropAuthority.SelectedValue);

                db.SaveChanges();
                MessageBox.Show("DONE");
            }
            catch (Exception ee)
            {
                if (ee.InnerException != null)
                    MessageBox.Show("Error: " + ee.InnerException.Message);
                else
                    MessageBox.Show("Error: " + ee.Message);

            }
        }

    }
}